# @todo youku  video phrase

import requests
from  express import jstopy
import re
import json
def main(id):
    url='https://acs.youku.com/h5/mtop.youku.favorite.query.isfavorite/1.0/?jsv=2.6.1&appKey=24679788&api=mtop.youku.favorite.query.isFavorite&type=originaljson&v=1.0&ecode=1&dataType=json&appName=pc_user_page&data=%7B%22targetType%22%3A%22SHOW%22%2C%22targetId%22%3A%22ccfc38c288e6485d92b1%22%2C%22appName%22%3A%22pc_user_page%22%2C%22appKey%22%3A%22Nn96sXA2qdCm5bUrTW%2Brnw%3D%3D%22%7D'
    r=requests.get(url)
    #
    cookies=r.cookies.get_dict()
    f = open('./vipcookie','r',encoding='utf-8')
    vip =f.read()
    f.close()
    headers = {
        'Cookie':'P_pck_rm='+vip+'; _m_h5_tk={cookies[_m_h5_tk]}; _m_h5_tk_enc={cookies[_m_h5_tk_enc]};'.format(cookies=cookies)
    }

    token=cookies['_m_h5_tk'].split('_')[0]
    #通过js文件获取url
    #@token
    #@id    视频id
    url=jstopy.execJs("./function.js","makeUrl",token,id)
    r=requests.get(url,headers=headers)
    data =re.search('mtopjsonp1\((.*)\)',r.text).groups()[0]
    data=json.loads(data)
    stream=data['data']['data']['stream']
    mau8 = stream[-1]["m3u8_url"] #列表最后一个清晰度最高
    print(mau8)
    r=requests.get(mau8)
    print(r.text)



main("XNTA1NDcyMDAyMA==")



def  getmo():
    url="http://valipl-vip.cp31.ott.cibntv.net/67756D6080932713CFC02204E/030009000060655F4C8F39DB700823E31B5059-D3A0-46B7-B9AE-0DD43C0B51F4-00278.ts?ccode=0502&duration=2760&expire=18000&psid=9339342b14983fc531a90ba3bc2eedd543346&ups_client_netip=b45ba40a&ups_ts=1629592328&ups_userid=1559397956&utid=oH2JGeZN%2B08CAbRbpAwxrhw7&vid=XNTA1NDcyMDAyMA%3D%3D&sm=1&operate_type=1&dre=u38&si=78&eo=0&dst=1&iv=1&s=ccfc38c288e6485d92b1&type=mp4hd3v3&bc=2&t=857999b546a2c64&cug=1&rid=20000000FD2C764317FA184C532A1595CF0DE4A702000000&vkey=B7b2ee4a02fe184a6c4ad7583af5e1a43"
    r=requests.get(url)
    f=open("1.ts",'wb')
    f.write(r.content)
    f.close()
    print(r.text)
# test()

